package com.example.intenttrial.adapters;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.intenttrial.BitmapOptimization;
import com.example.intenttrial.EmptyActivity;
import com.example.intenttrial.GalleryActivity;
import com.example.intenttrial.HandlerAndThreadActivity;
import com.example.intenttrial.MyYouTubeActivity;
import com.example.intenttrial.NetworkingActivity;
import com.example.intenttrial.PlayerActivity;
import com.example.intenttrial.ProgressActivity;
import com.example.intenttrial.R;
import com.example.intenttrial.RequestActivity;
import com.example.intenttrial.SDCardActivity;
import com.example.intenttrial.VimeoVideoActivity;
import com.example.intenttrial.bezier.BezierActivity;
import com.example.intenttrial.receiver.BroadcastReceiveActivity;
import com.example.intenttrial.receiver.BroadcastSendActivity;

public class StartListAdapter extends BaseAdapter{

	private static final String[] items = {"Vimeo", "YouTube", "Player", "Request", "Empty", 
		"SDCard", "Progress and wifi", "Uninstall", 
		"Bitmap Optimization", "pdf", "Gallery", 
		"Networking", "SEND broadcast", "Receive broadcast", "Handler and Thread", "Bezier"};
	
	private LayoutInflater inflater;
	private Context context;
	
	public StartListAdapter(Context context) {
		this.context = context;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public int getCount() {
		return items.length;
	}

	@Override
	public Object getItem(int pos) {
		return items[pos];
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int pos, View view, ViewGroup viewGroup) {
		
		if(view == null) {
			view = (View) inflater.inflate(R.layout.listitem_startlist, null); 
		}
		
		TextView tv = (TextView)view.findViewById(R.id.tv_activityname);
		
		tv.setText(items[pos]);

		view.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String text = ((TextView)v.findViewById(R.id.tv_activityname)).getText().toString();
				if(text == items[0]) {
					context.startActivity(new Intent(context, VimeoVideoActivity.class));
				} else if(text == items[1]) {
					context.startActivity(new Intent(context, MyYouTubeActivity.class));
				} else if(text == items[2]) {
					context.startActivity(new Intent(context, PlayerActivity.class));
				} else if(text == items[3]) {
					context.startActivity(new Intent(context, RequestActivity.class));
				} else if(text == items[4]) {
					context.startActivity(new Intent(context, EmptyActivity.class));
				} else if(text == items[5]) {
					context.startActivity(new Intent(context, SDCardActivity.class));
				} else if(text == items[6]) {
					context.startActivity(new Intent(context, ProgressActivity.class));
				} else if(text == items[7]) {
					uninstallApp();
				} else if(text == items[8]) {
					context.startActivity(new Intent(context, BitmapOptimization.class));
				} else if(text == items[9]) {
					String url = "/storage/sdcard0/Android/data/com.intuition.learn/files/.perm/3813/Manager Playbook_ASTART_Planning - Mobilize Team.pdf";
					url = "/storage/sdcard0/Download/Manager Playbook_Planning - Terms of Engagement.pdf";
					File f = new File(url);
					Uri uri = Uri.fromFile(f);
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					intent.setDataAndType(uri, "application/pdf");
					context.startActivity(intent);
				} else if(text == items[10]) {
					context.startActivity(new Intent(context, GalleryActivity.class));
				} else if(text == items[11]) {
					context.startActivity(new Intent(context, NetworkingActivity.class));
				} else if(text == items[12]) {
					context.startActivity(new Intent(context, BroadcastSendActivity.class));
				} else if(text == items[13]) {
					context.startActivity(new Intent(context, BroadcastReceiveActivity.class));
				} else if(text == items[14]) {
					context.startActivity(new Intent(context, HandlerAndThreadActivity.class));
				} else if(text == items[15]) {
					context.startActivity(new Intent(context, BezierActivity.class));
				}
				
			}
		});
		
		return view;
	}

	private void uninstallApp() {
		String packageName = context.getPackageName();
		Uri packageUri = Uri.parse("package:" + packageName);
		Intent uninstallIntent =
				new Intent(Intent.ACTION_DELETE, packageUri);
		uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(uninstallIntent);
	}
	
	
}
